package My.project.Ui.Activity;

import My.project.Entity.User;
import My.project.Ui.R;
import My.project.Ui.TabListeners;
import My.project.Ui.Fragment.LiveVideoFragment;
import My.project.Ui.Fragment.VideoFragment;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * This activity is the main activity displayed when application starts.
 * It displays all the live videos and recorded videos in two tabs,
 * implemented in fragments.
 * In its menu bar, the user can either record a video, search a video or
 * see his friends and other users.
 * @author Bell
 *
 */
public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//allow this application to use the network
		StrictMode.setThreadPolicy(
				new StrictMode.ThreadPolicy.Builder().detectDiskReads()
				.detectDiskWrites().detectNetwork().penaltyLog().build());  
        StrictMode.setVmPolicy(
        		new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
        		.detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); 
	}

	@Override
	protected void onStart() {
		super.onStart();
		// setup action bar for tabs
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.removeAllTabs();

        //live video tab
        Tab tab = actionBar.newTab()
                .setText(R.string.tab_livev)
                .setTabListener(new TabListeners<LiveVideoFragment>(
                        this, "live video", LiveVideoFragment.class));
        actionBar.addTab(tab);

        //ordinary video tab
        tab = actionBar.newTab()
            .setText(R.string.tab_v)
            .setTabListener(new TabListeners<VideoFragment>(
                    this, "video", VideoFragment.class));
        actionBar.addTab(tab);
	}


	//menu button action
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.str_about);
		menu.add(0, 1, 1, R.string.str_exit);
		// show menu
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionsDialog();
			break;
		case R.id.menu_record:
			startRecord();
			break;
		case R.id.menu_search:
			searchVedio();
			break;
		case R.id.menu_social:
			social();
			break;
		default:
			break;
		}
		return true;
	}
	
	// start record, if the user isn't log in, need to log in first
	private void startRecord() {
		Intent intent = new Intent();
		if (User.isLogin()) {
			intent.setClass(MainActivity.this, RecordActivity.class);
			startActivity(intent);
		} else {
			intent.setClass(MainActivity.this, LogInActivity.class);
			Bundle bundle = new Bundle();
    		bundle.putString("nextActivity", "record");
    		
    		intent.putExtras(bundle);
    		startActivity(intent);
		}
	}
	
	// start search video activity
	private void searchVedio() {
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, SearchActivity.class);
		
		startActivity(intent);
	}
	
	// show friends and other users, if the user isn't log in, need to log in first
	private void social() {
		Intent intent = new Intent();
		if (User.isLogin()) {
			intent.setClass(MainActivity.this, ListUserActivity.class);
    		startActivity(intent);
		} else {
			intent.setClass(MainActivity.this, LogInActivity.class);
			Bundle bundle = new Bundle();
    		bundle.putString("nextActivity", "friend");
    		
    		intent.putExtras(bundle);
			startActivity(intent);
		}
	}

	// show author info
	private void openOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_about)
		.setMessage(R.string.app_about_msg)
		.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		}).show();
	}

	// show exit dialog
	private void exitOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_exit)
		.setMessage(R.string.app_exit_msg)
		.setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				Intent intent = new Intent(Intent.ACTION_MAIN);
  	            intent.addCategory(Intent.CATEGORY_HOME);
  	            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  	            startActivity(intent);
  	            android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).show();
	}

}
